package com.ccys.cms.controller.administrator;

import com.ccys.cms.service.ModuleService;
import com.ccys.cms.service.RoleService;
import com.ccys.common.constants.ContextConstants;
import com.ccys.common.vo.ResponseResult;
import com.ccys.db.dto.web.base.RoleDto;
import com.ccys.db.vo.web.base.RoleSelectVO;
import com.ccys.db.vo.web.base.RoleVo;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import static com.ccys.common.util.Kit.*;


/**
 * 角色
 */
@RestController
@RequestMapping("/administrator")
@Api(tags = "系统-角色模块")
public class SysRoleController {

    @Resource
    private ModuleService moduleService;
    @Resource
    private RoleService roleService;

    @GetMapping("/role")
    @ApiOperation("获取角色列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleName", value = "角色名称",paramType = "query"),
            @ApiImplicitParam(name = "groupId", value = "集团ID",paramType = "query"),
            @ApiImplicitParam(name = "status", value = "状态",paramType = "query"),
            @ApiImplicitParam(name = "startTime", value = "开始时间(格式 yyyy-mm-dd)", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "结束时间(格式 yyyy-mm-dd)", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "orderByStr", value = "排序字段（后端返回的字段  asc升序，desc 降序,加上end后缀。示例（createdOn降序排 则传值 createdOn descend））",paramType = "query"),
    })
    @ApiResponses({
            @ApiResponse(code = 200,message = ContextConstants.MODEL_DESC,response = RoleVo.class)
    })
    public ResponseResult manage( @ApiIgnore RoleDto roleDto) {
        return roleService.queryRoles(roleDto);
    }


    @GetMapping("/role/selectList")
    @ApiOperation("查询选择角色")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "groupId", value = "集团ID",paramType = "query"),
    })
    @ApiResponses({
        @ApiResponse(code = 200,message = ContextConstants.MODEL_DESC,response = RoleSelectVO.class)
    })
    public ResponseResult selectList( @ApiIgnore RoleDto roleDto) {
        return roleService.selectList(roleDto);
    }


    @PostMapping("/role")
    @ApiOperation("新增角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleName", value = "角色名",paramType = "query"),
            @ApiImplicitParam(name = "remarks", value = "描述",paramType = "query"),
            @ApiImplicitParam(name = "moduleIds", value = "模块集",paramType = "query"),
            @ApiImplicitParam(name = "groupId", value = "集团ID",paramType = "query"),
            @ApiImplicitParam(name = "status", value = "状态",paramType = "query"),
            @ApiImplicitParam(name = "remarks", value = "描述",paramType = "query"),
    })
    public ResponseResult insert(@ApiIgnore RoleDto roleDto) {
        allParamValid(roleDto,"roleName","remarks","moduleIds");
        return roleService.insertRole(roleDto);
    }

    @PutMapping("/role")
    @ApiOperation("修改角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "角色id",paramType = "query"),
            @ApiImplicitParam(name = "roleName", value = "角色名",paramType = "query"),
            @ApiImplicitParam(name = "remarks", value = "描述",paramType = "query"),
            @ApiImplicitParam(name = "moduleIds", value = "模块集",paramType = "query"),
            @ApiImplicitParam(name = "groupId", value = "集团ID",paramType = "query"),
            @ApiImplicitParam(name = "status", value = "状态",paramType = "query"),
            @ApiImplicitParam(name = "remarks", value = "描述",paramType = "query"),
    })
    public ResponseResult update(@ApiIgnore RoleDto roleDto) {
        allParamValid(roleDto,"id");
        return roleService.updateRole( roleDto);
    }

    @DeleteMapping("/role")
    @ApiOperation("删除角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "角色id",paramType = "query"),
    })
    public ResponseResult delete(@ApiIgnore RoleDto roleDto) {
        allParamValid(roleDto,"id");
        roleDto.setDelFlag(1);
        return roleService.updateRole( roleDto);
    }

    @GetMapping("/role/modules")
    @ApiOperation("获取模块")
    public ResponseResult modules() {
        return moduleService.getCache();
    }
}
